<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

/**
 * 后台内容控制器
 * 
 * @author huajie <banhuajie@163.com>
 */
class HelpController extends AdminController
{

    public function index($pid = 0) {
        $M_article = M('Help');
        
        if ($pid) {
            $info = $M_article->find($pid);
            !$info && $this->error('参数错误');
        } 
        
        $map['pid'] = $pid;
        
        $list = $this->lists("Help", $map, 'sort asc');
        
        $this->assign('pid', $pid);
        $this->assign('_list', $list);
        $this->meta_title = "文章列表";
        $this->display();
    }
    
    public function add($pid = 0){
        $M_article = M('Help');
    
        if ($pid) {
            $parent_info = $M_article->find($pid);
            !$parent_info && $this->error('非法参数');
        }
    
        if (IS_POST) {
            $data = params_request(array('pid', 'title', 'sort', 'url', 'description'));
            
            $data['pid'] && !$M_article->find($data['pid']) && $this->error('非法参数');
            !preg_match('/^.{2,50}$/', $data['title']) && $this->error('标题长度必须在4~60个字符');
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;            
            $id = $M_article->add($data);
            
            $id > 0 ? $this->success('新增成功', U('index', array('pid'=>$pid))) : $this->error('新增失败，未知错误');
        } else {            
            $p_tree = array(0 => array('id' => 0,'title' => '顶级分类'));
            
            $cllist = M("help")->select();
            if ($cllist) {
                $p_tree = array_merge($p_tree, $cllist);
            }
            
            $p_tree = D('Common/Tree')->toFormatTree($p_tree, 'title', 'id', 'pid');
            $this->assign('plist', $p_tree);
            
            $this->assign('pid', $pid);
            $this->meta_title = '新增文章';
            $this->display('edit');
        }
    }

    public function edit($pid = 0){
        $id = I('request.id');
        
        $M_article = M('Help');
        
        $info = $M_article->find($id);
        !$info && $this->error('非法参数');
        if ($pid) {
            $parent_info = $M_article->find($pid);
            !$parent_info && $this->error('非法参数');
        }
        
        if (IS_POST) {
            $data = params_request(array('pid', 'title', 'sort', 'url', 'description'));
            
            $data['pid'] && !$M_article->find($data['pid']) && $this->error('非法参数');
            !preg_match('/^.{2,50}$/', $data['title']) && $this->error('标题长度必须在4~60个字符');
            
            $data['update_time'] = NOW_TIME;
            
            $status = $M_article->where(array('id'=>$id))->save($data);
            
            $status ? $this->success('编辑成功', U('index', array('pid'=>$pid))) : $this->error('编辑失败，未知错误');
        } else {            
            $p_tree = array(0 => array('id' => 0,'title' => '顶级分类'));
            
            $cllist = M("help")->select();
            if ($cllist) {
                $p_tree = array_merge($p_tree, $cllist);
            }
            
            $p_tree = D('Common/Tree')->toFormatTree($p_tree, 'title', 'id', 'pid');
            $this->assign('plist', $p_tree);
            
            $this->assign('info', $info);
            $this->assign('pid', $info['pid']);
            $this->meta_title = '编辑文章';
            $this->display('edit');
        }
    }

    public function delete() {
        $ids = I('id');
        
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        
        $M_article = M('Help');
        
        $map    =   array();
        
        if(is_array($ids)){
            $map['id|pid'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id|pid'] = $ids;
        }
        
        $M_article->where($map)->delete()!==false ? $this->success('删除成功！','',IS_AJAX) : $this->error('删除失败！','',IS_AJAX);
        
    }
}
